<?php
// prevent the server from timing out
set_time_limit(0);

// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';

// when a client connects
function wsOnOpen($clientID)
{
	global $Server;
	$ip = long2ip( $Server->wsClients[$clientID][6] );
	
	$Server->log( "$ip ($clientID) has connected." );
}

// when a client closes or lost connection
function wsOnClose($clientID, $status) {
	global $Server;
	$ip = long2ip( $Server->wsClients[$clientID][6] );

	$Server->log( "$ip ($clientID) has disconnected." );
	
	//Send a user left notice to everyone in the room
	foreach ( $Server->wsClients as $id => $client ){
		if ( $id != $clientID )
			send_message($id, 1, "User <b>{$Server->wsClientName[$clientID]}</b> has left the room.");
	}
	//clear client name after broadcast leaving message
	unset($Server->wsClientName[$clientID]);
}

// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
	global $Server;
	$ip = long2ip( $Server->wsClients[$clientID][6] );

	// check if message length is 0
	if ($messageLength == 0) {
		$Server->wsClose($clientID);
		return;
	}
		
	//message format: [header||content]
	//	client message 
	//		register:1
	//		message:2
	//		userlist:3
	//	server message
	//		server_message:1
	//		user_message:2
	//		user list:3
	
	$user_request = explode("||",$message);
	
	if($user_request[0]==$Server::WS_USER_REGISTER){
		//register client name
		$Server->wsClientName[$clientID] = $user_request[1];
		send_message($clientID, 1, "Welcome! ".$Server->wsClientName[$clientID]);
		//Send a join notice to everyone but the person who joined
		foreach ( $Server->wsClients as $id => $client ){
			if ( $id != $clientID ){
				send_message($id, 1, "User <b>{$Server->wsClientName[$clientID]}</b> has joined the room.");
				//refresh userlist
				$list = user_list_create();
				send_message($id, 3, $list["count"]."||".$list["list"]);
			}
		}
	}
	else{
		//not allow any request before regist
		if(isset($Server->wsClientName[$clientID]))
			$clientName = $Server->wsClientName[$clientID];
		else{
			send_message($clientID, 1, "Please register first!");
			return;
		}
		
		if($user_request[0]==$Server::WS_USER_MESSAGE){//normal message
			foreach ( $Server->wsClients as $id => $client ){
				send_message($id, 2, $Server->wsClientName[$clientID], $user_request[1]);
			}
		}
		else if($user_request[0]==$Server::WS_USER_LIST){//request user list
			$users = "";
			$userCount = 0;
			$list = user_list_create();
			send_message($clientID, 3, $list["count"]."||".$list["list"]);
		}
	}
}

function send_message(){
	$args=func_get_args();

	global $Server;
	$Server->wsSend($args[0], $args[1].((isset($args[3]))?"||".$args[3]:"")."||".$args[2]);
}

function user_list_create(){
	global $Server;
	foreach( $Server->wsClientName as $name ){
		if($userCount>0)
			$users .= "||";
		$users .= $name;
		$userCount++;
	}
	$user_list = array("count" => $userCount,
		"list" => $users);
	return $user_list;
}

// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('linkftp.myftp.org', 12345);

?>